package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础信息自动填充
 *
 * @author xudm
 */
@Slf4j
public class BasicMetaObjectHandler implements MetaObjectHandler {

    public static final String CREATED_BY = "createdBy";
    public static final String CREATED_TIME = "createdTime";
    public static final String UPDATED_TIME = "updatedTime";
    public static final String UPDATED_BY = "updatedBy";
    public static final String DELETE_FLAG = "deleteFlag";
    public static final String INSERT_FILL_ERROR_LOG = "自动填充值发生异常。{}";
    public static final String NO_LOGIN_ERR_INFO = "用户未登录，无法自动填充更新信息。";

    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUser user = SecurityUtils.getLoginUser();
        if (user == null) {
            log.warn(NO_LOGIN_ERR_INFO);
            return;
        }
        // 定义需要填充的字段和默认值
        Map<String, Object> fieldsToFill = new HashMap<>();
        fieldsToFill.put(CREATED_TIME, LocalDateTime.now());
        fieldsToFill.put(CREATED_BY, user.getUserId());
        fieldsToFill.put(UPDATED_BY, user.getUserId());
        fieldsToFill.put(UPDATED_TIME, LocalDateTime.now());
        fieldsToFill.put(DELETE_FLAG, 0);
        try {
            // 遍历填充字段
            for (Map.Entry<String, Object> entry : fieldsToFill.entrySet()) {
                String fieldName = entry.getKey();
                Object fieldValue = entry.getValue();
                Object currentValue = this.getFieldValByName(fieldName, metaObject);
                if (currentValue == null) {
                    this.setFieldValByName(fieldName, fieldValue, metaObject);
                }
            }
        } catch (Exception e) {
            log.error(INSERT_FILL_ERROR_LOG, e.getMessage());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUser user = SecurityUtils.getLoginUser();
        if (user == null) {
            log.warn(NO_LOGIN_ERR_INFO);
            return;
        }
        try {
            // 定义需要填充的字段和值
            Map<String, Object> fieldsToUpdate = new HashMap<>();
            fieldsToUpdate.put(UPDATED_TIME, LocalDateTime.now());
            fieldsToUpdate.put(UPDATED_BY, user.getUserId());

            // 遍历填充字段
            for (Map.Entry<String, Object> entry : fieldsToUpdate.entrySet()) {
                String fieldName = entry.getKey();
                Object fieldValue = entry.getValue();
                this.setFieldValByName(fieldName, fieldValue, metaObject);
            }
        } catch (Exception e) {
            log.error("自动更新值发生异常。{}", e.getMessage());
        }
    }
}
